layui.config({
  base: '/' //假设这是你存放拓展模块的根目录
}).extend({ //设定模块别名
  murl: 'url' //如果 mymod.js 是在根目录，也可以不用设定别名
});
layui.use(['form','murl','layer','element','laydate','laytpl'], function () {
     var laydate = layui.laydate,laytpl = layui.laytpl ,form = layui.form;
     var layerTips = parent.layer === undefined ? layui.layer : parent.layer;

     $.get ('/tender/getInfo' , null, function (data) {
         if(data.rel){
            var result = data.result;
            initKgHtml(result,"#blockui_equity_portlet_body","#tempHtml")
         }
     });

     form.on('submit(buy)', function(data){
            var id=$(this).attr("id");
           // alert($("#name_"+id).text());
            buy(id,$("#name_"+id).text(),$("#price_"+id).text());
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
    });
});

function initKgHtml(datas,element,template){
  for(var i =0; i< datas.length; i++){
      var baseHtml=$(template).html();
     baseHtml= baseHtml
     .replace("#PIC",datas[i].litpic)
     .replace("#NAME",datas[i].name)
     .replace("#PERIOD",datas[i].borrowPeriod)
     .replace("#MIN",datas[i].min)
     .replace("#PRICE",datas[i].price)
     .replace("#APR",datas[i].borrowApr)
     .replace("#ID",datas[i].id)
     .replace("#NID",datas[i].id)
     .replace("#PID",datas[i].id)
      .replace("#CONTENT",datas[i].borrowContents);
      $(element).append(baseHtml);
  }
}

function submitForm(data){
  $.ajax({
     url: '/money/wk',
     type: 'post',
     data: data.field,
     dataType: "json",
     success: function (data) {
         var layerTips = parent.layer === undefined ? layui.layer : parent.layer;
         if(data.rel){
            layerTips.msg('购买成功');
            parent.queryMoney();
         }else{
           layerTips.msg(data.msg);
         }
     }
   });
}

function setLabelValues(data) {
    for (var p in data) {
        $("#" + p + "").html(data[p]);
    }
}

function setInputValues(data) {
    for (var p in data) {
        $("#" + p + "").val(data[p]);
    }
}

function buy(id,name,price){
  $.get('/wk/edit', null, function (form) {
  layer.open({
        type: 1,
        title: '购买'+name,
        content: form,
        btn: ['购买', '取消'],
        shade: false,
        offset: ['20px', '20%'],
        area: ['600px', '230px'],
        maxmin: false,
        yes: function (index) {
            //触发表单的提交事件

            $('form.layui-form').find('button[lay-filter=edit]').click();
        },

        success: function (layero, index) {
              var form = layui.form;
              $("#id").val(id);
               $("#zcCount").val(price);
            form.on('submit(edit)', function (data) {
                $.ajax({
                    url:  "/tender/buy" ,
                    type: 'put',
                    data: data.field,
                    dataType: "json",
                    success: function (data) {
                       if(data.rel){
                         layer.msg('购买成功');
                         parent.queryMoney();
                         layer.close(index);
                       }else{
                         layer.msg(data.msg);
                       }

                        //location.reload();
                       // layer.close(editIndex);
                    }

                });
                //这里可以写ajax方法提交表单
                return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
            });
        }
    });
 });
}